﻿/********************************************************************************************
 * 文件名称:	List.cs
 * 设计人员:	yan(yanxianghui@gmail.com)
 * 设计时间:	2009-03-16
 * 功能描述:	豆列功能
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.Model;
using Shanfree.SharpMovie.IDAL;

namespace Shanfree.SharpMovie.SQLServerDAL
{
    /// <summary>
    /// 豆列功能
    /// </summary>
    public class List : DBPersistenceBase<ListInfo>, IList
    {

        #region SQL
        private const string SQL_SELECT_ALL = @"SELECT *
                                                                              FROM [dbo].[V_List]";
        private const string SQL_SELECT_USERID = @"SELECT *
                                                                              FROM [dbo].[V_List] WHERE UserID = @UserID";
        private const string SQL_SELECT_Name = @"SELECT *
                                                                              FROM [dbo].[V_List] WHERE Name = @Name";
        private const string SQL_SELECT_CATALOGID = @"SELECT *
                                                                              FROM [dbo].[V_List] WHERE CatalogID = @CatalogID";
        private const string SQL_SELECT_CATALOGID_RECURSION = @"SELECT L.*
                                                                                FROM [dbo].[V_List] L,
                                                                                (
	                                                                                SELECT ID
	                                                                                FROM [Catalog]
	                                                                                WHERE 
	                                                                                Code like '{0}%'
                                                                                ) AS C
                                                                                WHERE L.CatalogID = C.ID";
        #endregion

        #region 数据库连接串
        /// <summary>
        /// 数据库连接串
        /// </summary>
        protected override string ConnectionString
        {
            get
            {
                return DBConnection.ConnectionString;
            }
        }
        #endregion

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns></returns>
        public IList<ListInfo> GetList(int pageSize, int pageIndex)
        {
            string pagedSQL = CommonHelper.GetPagedSQL(SQL_SELECT_ALL, pageSize, pageIndex);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, pagedSQL);
            IList<ListInfo> list = GetListFromReader(reader);
            CustomList<ListInfo> cList = PageAmoutHelper<ListInfo>.GetCutomList(this.ConnectionString, SQL_SELECT_ALL, pageSize, list);
            return cList;
        }

        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="movieID">用户主键</param>
        /// <returns></returns>
        public IList<ListInfo> GetList(string userID, int pageSize, int pageIndex)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@UserID", userID);
            CustomList<ListInfo> cList =
                GetCustomerListByParamList(this.ConnectionString, SQL_SELECT_USERID, param, pageSize, pageIndex);
            return cList;
        }
        /// <summary>
        /// 根据列表
        /// </summary>
        /// <param name="movieID">用户主键</param>
        /// <param name="isRecursion">是否递归获取子分类下所有列表</param>
        /// <returns></returns>
        public IList<ListInfo> GetListByCatalogID(string catalogID, bool isRecursion, int pageSize, int pageIndex)
        {
            CatalogInfo cInfo = new Catalog().GetByID(catalogID);
            string sql = string.Format(SQL_SELECT_CATALOGID_RECURSION, cInfo.Code);
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@CatalogID", catalogID);
            CustomList<ListInfo> cList = new CustomList<ListInfo>();
            if (isRecursion)
            {
                cList = GetCustomerListByParamList(this.ConnectionString, sql, pageSize, pageIndex);
            }
            else
            {
                cList = GetCustomerListByParamList(this.ConnectionString, SQL_SELECT_CATALOGID, param, pageSize, pageIndex);
            }
            return cList;
        }

        /// <summary>
        /// 根据名称获取列表
        /// </summary>
        /// <param name="name">列表名称</param>
        /// <returns></returns>
        public ListInfo GetByName(string name)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@Name", name);
            SqlDataReader reader = SqlHelper.ExecuteReader(this.ConnectionString, CommandType.Text, SQL_SELECT_Name, param);
            IList<ListInfo> list = GetListFromReader(reader);
            if (null == list || list.Count == 0)
            {
                return new ListInfo();
            }
            return list[0];
        }


        /// <summary>
        /// 写入扩展属性
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        protected override ListInfo WriteExtendProperty(SqlDataReader reader, ListInfo t)
        {
            t.UserName = reader["UserName"].ToString();
            return t;
        }

    }
}
